package com.weiaibenpao.demo.chislim.ui.activity.discover;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.MallBean;
import com.weiaibenpao.demo.chislim.presenter.DjqMallPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.DjqMallAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.DjqMallView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MallActivity extends BaseActivity<DjqMallView, DjqMallPresenter> implements DjqMallView,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener,SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.myMatch)
    RecyclerView myMallRecyclerView;
    @BindView(R.id.mid_text)
    TextView midText;
    DjqMallAdapter djqMallAdapter;
    int page = 1;

    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private List<MallBean.ListBean> mallBeanList;
    private View view;

    /**
     * 发现 - 运动商城
     */

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_match;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        midText.setText("运动商城");
        initAdapter();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setRefreshing(true);
        doRefresh();
//        DjqMallAdapter.setOnItemClickListener(new DjqMallAdapter.OnItemClickListener() {
//            @Override
//            public void setOnItemClick(View view, String h5_url) {
//                Logger.e("ljb:" + h5_url);
//                Intent intent = new Intent(MallActivity.this, WebViewActivity.class);
//                intent.putExtra("h5_url", h5_url);
//                intent.putExtra("h5_title", "健身商城");
//                startActivity(intent);
//            }
//        });
    }
    private void doRefresh() {
        page=1;
        djqMallAdapter.setEnableLoadMore(false);
        doRequest();
    }
    private void initAdapter() {
        view = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        djqMallAdapter = new DjqMallAdapter();
        djqMallAdapter.setOnItemClickListener(this);
        djqMallAdapter.setOnLoadMoreListener(this,myMallRecyclerView);
        myMallRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        myMallRecyclerView.setAdapter(djqMallAdapter);

    }

    @Override
    public void djqMallSuccess(MallBean mallBean) {
        swipeLayout.setRefreshing(false);
        djqMallAdapter.setEmptyView(view);
        boolean isRefresh=page==1;
        setData(mallBean.getList(), isRefresh);
        mallBeanList = mallBean.getList();
    }

    private void setData(List<MallBean.ListBean> marathoList, boolean isRefresh) {
        int size = marathoList != null ? marathoList.size() : 0;
        if (page == 1) {
            djqMallAdapter.setNewData(marathoList);

        } else {
            djqMallAdapter.addData(marathoList);
            djqMallAdapter.loadMoreComplete();
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            djqMallAdapter.loadMoreEnd();
        } else {
            djqMallAdapter.loadMoreComplete();
        }
    }

    @Override
    public DjqMallPresenter initPresenter() {
        return new DjqMallPresenter();
    }

    @OnClick(R.id.left_image)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void onLoadMoreRequested() {

        page++;
        doRequest();
    }

    private void doRequest() {
        mPresenter.getDjqMall(page, Constant.DEFAULT_PAGE_SIZE);

    }

    @Override
    public void onRefresh() {
        doRefresh();
    }


//    @Override
//    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//
//
////        Intent intent = new Intent(MallActivity.this, WebViewActivity.class);
////        intent.putExtra("h5_url", djqMallAdapter.getItem(position).getTmallUrl());
////        intent.putExtra("h5_title", "健身商城");
////        startActivity(intent);
//    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        WebViewActivity.startSelf(this,djqMallAdapter.getItem(position).getTmallUrl(),"运动商城",1);
    }
}
